package com.zs.leymovie.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.zs.leymovie.R;

/**
 * 显示百度地图
 */
public class MapActivity extends AppCompatActivity {

        private MapView mapView;
        private BaiduMap baiduMap;
        private double lng = 114.058000;  //默认深圳
        private double lat =22.543800;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            initView();
            setContentView(mapView);
            addMarkOverlay();
        }
    private void addMarkOverlay() {
        MarkerOptions options = new MarkerOptions();
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.point);
        options.icon(bitmapDescriptor);
        LatLng latLng = new LatLng(lat, lng);
        options.position(latLng);
        baiduMap.addOverlay(options);
    }

    public void initView(){
        Bundle bundle = getIntent().getExtras();
        if(bundle !=null){
            lng = bundle.getDouble("lng",lng);
            lat = bundle.getDouble("lat",lat);
        }
        BaiduMapOptions options = new BaiduMapOptions();
        LatLng latLng = new LatLng(lat, lng);
        MapStatus.Builder builder = new MapStatus.Builder().target(latLng).zoom(17f);  //缩放级别
        options.mapStatus(builder.build());
      /*options.compassEnabled(true);   //默认是true
        options.zoomControlsEnabled(true);//默认是true
        options.zoomGesturesEnabled(true);//默认是true*/
        mapView = new MapView(this,options);   //通过构造方法的方法创建Map
        baiduMap = mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);  //2D显示
        baiduMap.setMaxAndMinZoomLevel(3, 20);   //最小与最大的缩放级别
        baiduMap.setTrafficEnabled(true);
    }

    /**
     * 退出释放网络资源
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(baiduMap!=null){
            baiduMap.clear();
            baiduMap = null;
        }
        if(mapView !=null){
            mapView.onDestroy();
            mapView = null;
        }
    }
}
